var xhr;


username.onblur = function () {
    var usernames = document.getElementById("username").value;
    xhr = new XMLHttpRequest();
    xhr.open("GET", "usernameIsExist?username=" + usernames, true);
    console.log(usernames);
    xhr.onreadystatechange = process;
    xhr.send(null);
}

password.onblur = function () {
    var password = document.getElementById('password').value;
    var msg = document.getElementById('passwordAvailableInfo');
    if (password === '') {
        msg.className = 'errormsg';
        msg.innerText = '密码不能为空';
    } else {
        //todo 密码可用? 比如大于几位 小于几位 得大小写???
        msg.className = 'okmsg';
        msg.innerText = '密码可用';
    }
}

password2.onblur = function () {
    var password = document.getElementById('password').value;
    var password2 = document.getElementById('password2').value;
    var msg = document.getElementById('repeatpassword');
    if (password !== password2) {
        msg.classList.remove('okmsg');
        msg.classList.add('errormsg')
        msg.innerText = '两次输入密码不一致';
    } else {
        msg.classList.remove('errormsg');
        msg.classList.add('okmsg');
        msg.innerText = '两次密码一致';
    }
}


function process() {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            var responInfo = xhr.responseText;
            var msg = document.getElementById('isExistInfo');
            if (responInfo === 'Exist') {
                msg.classList.remove('okmsg');
                msg.classList.add('errormsg');
                msg.innerText = '用户名不可用';
            } else if (responInfo === 'Not Exist') {
                msg.classList.remove('errormsg');
                msg.classList.add('okmsg');
                msg.innerText = '用户名可用';
            } else if (responInfo === 'Empty') {
                msg.classList.remove('okmsg');
                msg.classList.add('errormsg');
                msg.innerText = '用户名不能为空'
            }
        }
    }
}


